#! /bin/bash

#上个月1日
Last_Month_DATE=$(date -d "$(date +%Y%m)01 last month" +%Y-%m-01)

#根据TD_DATE计算年季度月日
V_PARYEAR=`date --date="$Last_Month_DATE" +%Y`
V_PARMONTH=`date --date="$Last_Month_DATE" +%m`
V_PARDAY=`date --date="$Last_Month_DATE" +%d`

V_month_for_quarter=`date --date="$Last_Month_DATE" +%-m`
V_PARQUARTER=$(((${V_month_for_quarter}-1)/3+1))

#计算所需要的日期字符串
V_Month="${V_PARYEAR}"_"${V_PARMONTH}"
V_QUARTER="${V_PARYEAR}"_Q"${V_PARQUARTER}"
V_Year="${V_PARYEAR}"

${HIVE_HOME} -S -e "
--分区
SET hive.exec.dynamic.partition=true;
SET hive.exec.dynamic.partition.mode=nonstrict;
set hive.exec.max.dynamic.partitions.pernode=10000;
set hive.exec.max.dynamic.partitions=100000;
set hive.exec.max.created.files=150000;
--分桶
set hive.enforce.bucketing=true;
set hive.enforce.sorting=true;
set hive.optimize.bucketmapjoin = true;
set hive.auto.convert.sortmerge.join=true;
set hive.auto.convert.sortmerge.join.noconditionaltask=true;
--并行执行
set hive.exec.parallel=true;
set hive.exec.parallel.thread.number=16;
--小时
INSERT INTO itcast_dws.itcast_clue_dws partition (yearinfo)
select 
    clue_nums,
    t1.origin_type_stat,
    t1.for_new_user,
    t1.hourinfo,
    t1.dayinfo,
    t1.monthinfo,
    t1.yearinfo,
    '1' as time_type,
    CONCAT(t1.yearinfo,"-",t1.monthinfo,"-",t1.dayinfo," ",t1.hourinfo) as time_str
    from itcast_dwm.itcast_clue_dwm t1 
    where  CONCAT_WS('-',t1.yearinfo,t1.monthinfo,t1.dayinfo)>='${Last_Month_DATE}';


INSERT INTO itcast_dws.itcast_clue_dws partition (yearinfo)
select 
    clue_nums,
    t1.origin_type_stat,
    t1.for_new_user,
    '-1' as t1.hourinfo,
    t1.dayinfo,
    t1.monthinfo,
    t1.yearinfo,
    '2' as time_type,
    CONCAT(t1.yearinfo,"-",t1.monthinfo,"-",t1.dayinfo) as time_str
    from itcast_dwm.itcast_clue_dwm t1 
    where  CONCAT_WS('-',t1.yearinfo,t1.monthinfo,t1.dayinfo)>='${Last_Month_DATE}';
    group by t1.yearinfo,t1.monthinfo,t1.dayinfo,t1.origin_type_stat, t1.for_new_user;


INSERT INTO itcast_dws.itcast_clue_dws partition (yearinfo)
select 
    clue_nums,
    t1.origin_type_stat,
    t1.for_new_user,
    '-1' as t1.hourinfo,
    '-1' as t1.dayinfo,
    t1.monthinfo,
    t1.yearinfo,
    '4' as time_type,
    CONCAT(t1.yearinfo,"-",t1.monthinfo) as time_str
    from itcast_dwm.itcast_clue_dwm t1 
    where  CONCAT_WS('-',t1.yearinfo,t1.monthinfo)>='${V_Month}';
     group by t1.yearinfo,t1.monthinfo,t1.origin_type_stat, t1.for_new_user;

    INSERT INTO itcast_dws.itcast_clue_dws partition (yearinfo)
select 
    clue_nums,
    t1.origin_type_stat,
    t1.for_new_user,
    '-1' as t1.hourinfo,
    '-1' as t1.dayinfo,
    '-1' as t1.monthinfo,
    t1.yearinfo,
    '5' as time_type,
    t1.yearinfo as time_str
    from itcast_dwm.itcast_clue_dwm t1 
    where  t1.yearinfo >='${V_Year}'
    group by t1.yearinfo,t1.origin_type_stat, t1.for_new_user;